import { createStore } from "vuex";
import axios from "axios";
export default createStore({
  state() {
    return {
      yu: [],
      io: JSON.parse(localStorage.getItem("op")) || [],
      ik: ["综合排序", "销量优先", "价格"],
      num: 0,
      num1: 0,
      // 声明一个布尔值用于判断
      los1: false,
      arr: ["推荐", "母婴", "鞋包首饰", "食品", "数码家电", "居家百货"],
      hj: JSON.parse(localStorage.getItem("op")) || [],
      arr1: JSON.parse(localStorage.getItem("op1")) || [],
    };
  },
  mutations: {
    // 获取异步请求的数据
    huoqu(state, h) {
      state.hj = h;
      console.log(state.hj);
      localStorage.setItem("op", JSON.stringify(state.hj));
    },
    // 点击使其母婴发生改变
    dianji(state, v) {
      state.num = v;
      state.io = state.hj.filter((item) => item.type == v - 1);
      if (state.num == 0) {
        state.io = state.hj;
      }
      console.log(state, v);
    },
    // 点击销量优先
    dijia(state, l) {
      state.num1 = l;
      if (state.num1 == 0) {
        state.io = state.io.sort((a, b) => a.y - b.y);
        state.los1 = false;
        console.log(state.io);
      } else if (state.num1 == 1) {
        state.io = state.io.sort((a, b) => b.num - a.num);
        state.los1 = false;
      } else if (state.num1 == 2) {
        if (state.los1 == false) {
          state.io = state.io.sort((a, b) => a.price - b.price);
          state.los1 = true;
        } else if (state.los1 == true) {
          state.io = state.io.sort((a, b) => b.price - a.price);
          state.los1 = false;
        }
      }
    },
    // 点击加号
    diop(state, o) {
      let v = state.arr1.find((item) => item.id == o.id);
      if (!v) {
        state.arr1.push(o);
        console.log(state.arr1);
        localStorage.setItem("op1", JSON.stringify(state.arr1));
      } else {
        state.arr1.map((item1) => {
          if (item1.id == o.id) {
            item1.shu = item1.shu + 1;
          }
        });
        localStorage.setItem("op1", JSON.stringify(state.arr1));
      }
    },
    // 点击数量加号
    jia(state, o1) {
      if (state.arr1[o1].shu >= state.arr1[o1].stock) {
        state.arr1[o1].shu = state.arr1[o1].stock;
      } else {
        state.arr1[o1].shu++;
      }
      localStorage.setItem("op1", JSON.stringify(state.arr1));
    },
    // 点击数量减减
    jian(state, i1) {
      state.arr1[i1].shu--;
      if (state.arr1[i1].shu <= 1) {
        state.arr1[i1].shu = 1;
      }
      localStorage.setItem("op1", JSON.stringify(state.arr1));
    },
    // 点击checkbox改变
    bianhua1(state, ih) {
      state.arr1.map((item) => {
        if (item.id == ih) {
          item.done = !item.done;
        }
      });
      localStorage.setItem("op1", JSON.stringify(state.arr1));
    },
    // d点击编辑使其删除
    shanchu1(state) {
      state.arr1 = state.arr1.filter((item) => item.done == false);
      localStorage.setItem("op1", JSON.stringify(state.arr1));
    },
  },
  actions: {
    // 异步请求
    async ijs({ commit }) {
      let b = await axios.get("mock.json");
      console.log(b.data.data);
      commit("huoqu", b.data.data);
      localStorage.setItem("op", JSON.stringify(b.data.data));
    },
  },
  getters: {
    num1(state) {
      return state.arr1.filter((item) => item.done).length;
    },
    quan(state) {
      var c = 0;
      state.arr1.filter((item) => {
        if (item.done == true) {
          c += item.shu * item.price;
        }
      });
      return c;
    },
  },
});
